﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IHotPaths</summary>
	public class IHotPaths : IDisposable
	{
        protected System.Collections.Generic.List<string> FItems; // Zoznam

        /// <summary>Property: Count</summary>
        public int Count
        {
            get
            {
                return FItems.Count;
            }
        }
        /// <summary>Property: []</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>hodnota</returns>
        public string this[int pIndex]
        {
            get
            {
                return ((pIndex < 0) || (pIndex >= FItems.Count)) ? "" : FItems[pIndex];
            }
        }

        /// <summary>Ziskanie nazvu</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov alebo prazdny retazec</returns>
        public string GetName(int pIndex)
        {
            string[] S = this[pIndex].Split(new char[] { (char)0x1 });
            return S.Length > 0 ? S[0] : "";
        }
        /// <summary>Ziskanie cesty 1</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov alebo prazdny retazec</returns>
        public string GetPath1(int pIndex)
        {
            string[] S = this[pIndex].Split(new char[] { (char)0x1 });
            return S.Length > 1 ? (S[1] == "" ? null : S[1].Trim()) : "";
        }
        /// <summary>Ziskanie cesty 2</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov alebo prazdny retazec</returns>
        public string GetPath2(int pIndex)
        {
            string[] S = this[pIndex].Split(new char[] { (char)0x1 });
            return S.Length > 2 ? (S[2] == "" ? null : S[2].Trim()) : "";
        }
        /// <summary>Ziskanie poskytovatela 1</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov alebo prazdny retazec</returns>
        public string GetConnProvider1(int pIndex)
        {
            string[] S = this[pIndex].Split(new char[] { (char)0x1 });
            return S.Length > 3 ? (S[3] == "" ? null : S[3].Trim()) : "";
        }
        /// <summary>Ziskanie poskytovatela 2</summary>
        /// <param name="pIndex">pozicia</param>
        /// <returns>nazov alebo prazdny retazec</returns>
        public string GetConnProvider2(int pIndex)
        {
            string[] S = this[pIndex].Split(new char[] { (char)0x1 });
            return S.Length > 4 ? (S[4] == "" ? null : S[4].Trim()) : "";
        }

        /// <summary>Konstruktor</summary>
        public IHotPaths()
        {
            FItems = new System.Collections.Generic.List<string>();
        }
        /// <summary>Dispose metoda</summary>
        public void Dispose()
        {
            FItems = null;
        }

        /// <summary>Vymazanie obsahu</summary>
        public void Clear()
        {
            FItems.Clear();
        }
        /// <summary>Zaradenie zaznamu</summary>
        /// <param name="pName">nazov</param>
        /// <param name="pValue1">cesta cislo 1</param>
        /// <param name="pValue2">cesta cislo 2</param>
        public void Add(string pName, string pValue1, string pValue2, string pConnProvider1 = "", string pConnProvider2 = "")
        {
            string V1 = pValue1 == null ? "" : pValue1.Trim() + " ";
            string V2 = pValue2 == null ? "" : pValue2.Trim() + " ";
            string V11 = pConnProvider1 == null ? "" : pConnProvider1.Trim() + " ";
            string V21 = pConnProvider2 == null ? "" : pConnProvider2.Trim() + " ";

            string Sep = ((char)0x1).ToString();
            FItems.Add(pName.Trim() + Sep + V1 + Sep + V2 + Sep + V11 + Sep + V21); // Sformatujem a zaradim 
        }
        /// <summary>Vymazanie zaznamu</summary>
        /// <param name="pIndex">pozicia</param>
        public void Delete(int pIndex)
        {
            if ((pIndex < 0) || (pIndex >= FItems.Count)) return; // Test pozicie
            FItems.RemoveAt(pIndex); // Vymazem
        }

        /// <summary>Serializacia obsahu</summary>
        /// <param name="pEncode">priznak zakodovania</param>
        /// <returns>vysledok</returns>
        public string Serialize(bool pEncode = true)
        {
            System.Text.StringBuilder B = new System.Text.StringBuilder();
            foreach (string S in FItems)
            {
                B.Append(S); B.Append(Environment.NewLine);
            }
            return pEncode ? ICore.Base64Encode(B.ToString()) : B.ToString();
        }
        /// <summary>Deserializacia obsahu</summary>
        /// <param name="pData">serializovany obsah</param>
        /// <param name="pDecode">priznak dekodovania</param>
        public void Deserialize(string pData, bool pDecode = true)
        {
            try
            {
                string Data = pDecode ? ICore.Base64Decode(pData) : pData;
                using (System.IO.StringReader R = new System.IO.StringReader(Data)) // String reader
                {
                    while (true)
                    {
                        string Line = R.ReadLine();
                        if (Line == null) break; // Koniec?
                        if (string.IsNullOrWhiteSpace(Line)) continue; // Prazdny riadok?

                        FItems.Add(Line); // Zaradim
                    }
                }
            }
            catch // Chyba
            {
                FItems.Clear();
            }
        }
    }
}
